package com.qiantu.phone.ui.adapter;

import static com.qiantu.phone.BuildConfig.OBS_BASE;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.db.LLSceneDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.RoomBean;
import com.qiantu.api.entity.SceneBean;
import com.qiantu.api.utils.ThemeUtil;
import com.qiantu.phone.BuildConfig;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.glide.GlideApp;
import com.qiantu.phone.utils.GetJsonDataUtil;

import java.util.List;
import java.util.Map;

public class RoomListSmallAdapter extends AppAdapter<RoomBean> {
    public RoomListSmallAdapter(@NonNull Context context) {
        super(context);
    }

    @NonNull
    @Override
    public RoomListSmallAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new RoomListSmallAdapter.ViewHolder();
    }

    @Override
    public void setData(@Nullable List<RoomBean> data) {
        super.setData(data);
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {
        private ImageView closeImage;
        private ImageView roomIconImage;
        private TextView roomName;
        private TextView roomInfo;
        private ViewHolder() {
            super(R.layout.item_room_list_small);
            closeImage = findViewById(R.id.btn_all_close);
            roomIconImage = findViewById(R.id.room_icon_image);
            roomName = findViewById(R.id.tv_name);
            roomInfo = findViewById(R.id.tv_device_num);
        }

        @Override
        public void onBindView(int position) {
            RoomBean roomBean = getItem(position);
            if(roomBean.getLinkType() == -1){
                roomIconImage.setImageDrawable(AppApplication.getInstance().getThemeDrawable(R.attr.icon_room_floor));
            }else{
                String jsonDataStr = new GetJsonDataUtil().getJson(getContext(), "RoomType.json");//获取assets目录下的json文件数据
                Map roomIcon = new Gson().fromJson(jsonDataStr, new TypeToken<Map>() {}.getType());
                Map iconInfo = (Map) roomIcon.get(roomBean.getRoomTypeId());

                String imageUrl = BuildConfig.APP_IMAGE_BASE + "app/images/roomType/" + ThemeUtil.getIconThemeSuffix() + iconInfo.get("iconName");
                GlideApp.with(roomIconImage)
                        .load(imageUrl)
                        .into(roomIconImage);
            }
            roomName.setText(roomBean.getRoomName());
            if(roomBean.getRoomSerialNo() == null){
                roomBean.setDevices(LLDeviceDBManager.getInstance(getContext()).getAvailableDevices());
                roomBean.setScenes(LLSceneDBManager.getInstance(getContext()).getAll());
            }
            List<DeviceBean> devices = roomBean.getDevices();
            List<SceneBean> scenes = roomBean.getScenes();
            Boolean uncloseDevice = false;
            int deviceSize = 0;
            for(DeviceBean deviceBean : devices){
                if(!deviceBean.getIsDisable()){
                    deviceSize++;
                }
                if(deviceBean.getIsOpen() && deviceBean.getType().equals("light") && deviceBean.getDeviceGroup() == 0){
                    uncloseDevice = true;
                }
            }
            closeImage.setVisibility(uncloseDevice ? View.VISIBLE : View.GONE);
            int sceneSize = scenes == null ? 0 : scenes.size();
            String text = "";
            if(deviceSize > 0){
                text = getString(R.string.device_count_holder, deviceSize);
            }
            if(sceneSize > 0){
                if(TextUtils.isEmpty(text)){
                    text = getString(R.string.scene_count_holder, sceneSize);
                }else{
                    text += "，" + getString(R.string.scene_count_holder, sceneSize);
                }
            }
            if(TextUtils.isEmpty(text)){
                text = "暂无设备和场景";
            }
            roomInfo.setText(text);
        }
    }

    @Override
    public long getItemId(int position) {
        return getData().get(position).hashCode();
    }
}
